package com.duzhuo.springsecurity.controller;

import com.duzhuo.springsecurity.config.TokenManager;
import com.duzhuo.springsecurity.entity.PhoneCodeAuthentication;
import com.duzhuo.springsecurity.handler.PhoneCodeAuthenticationProvider;
import com.duzhuo.springsecurity.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 15714
 */
@Controller
public class OneController {
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    TokenManager tokenManager;


    @RequestMapping("/user")
    @ResponseBody
    public Authentication userDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

    @RequestMapping("/clean")
    @ResponseBody
    public void clean() {
         SecurityContextHolder.clearContext();
    }

    @RequestMapping("/one")
    @ResponseBody
    @PreAuthorize(value = "hasAuthority('vip1')")
    public String one() {
        return "001";
    }


    @RequestMapping("/main")
    public String main() {
        return "/main.html";
    }


    @RequestMapping("/sucess")
    @ResponseBody
    @PreAuthorize(value = "hasAuthority('xal')")
    public String sucess() {
        return "/demo";
    }

    @RequestMapping("/error1")
    public String error() {
        return "error";
    }

    @RequestMapping("/ok")
    @ResponseBody
    public String ok() {
        return "OK";
    }


//    @RequestMapping("/login")
//    @ResponseBody
//    public String mylogin(@RequestBody LoginVo loginVo) {
//        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginVo.getUserName(), loginVo.getPassword());
//        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
//        Assert.notNull(authenticate, "authenticate is null");
//        UserDetails principal =(UserDetails) authenticate.getPrincipal();
//
//
//        List<String> list = new ArrayList<>();
//        list.add("admin");
//        list.add("vip1");
//        list.add("vip2");
//        HashMap<String, Object> map = new HashMap<>();
//        map.put(TokenManager.ROLE_CLAIMS,list);
//        String token = tokenManager.createToken(loginVo.getUserName(), map);
//        return "token="+token;
//    }


    @RequestMapping("/myPhoneLogin")
    @ResponseBody
    public String phoneLogin(@RequestBody LoginVo loginVo) {
        PhoneCodeAuthentication authenticationToken = new PhoneCodeAuthentication(loginVo.getPhone(), loginVo.getCode(),new ArrayList());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        Assert.notNull(authenticate, "authenticate is null");
        UserDetails principal =(UserDetails) authenticate.getPrincipal();


        List<String> list = new ArrayList<>();
        list.add("admin");
        list.add("vip1");
        list.add("vip2");
        HashMap<String, Object> map = new HashMap<>();
        map.put(TokenManager.ROLE_CLAIMS,list);
        String token = tokenManager.createToken(loginVo.getUserName(), map);
        return "token="+token;
    }

}
